import { createStore } from 'vuex';

export default createStore({
    state() {
        return {
            language: window.localStorage.getItem('lang') || 'zh-CN',
            articleTree: [],
            currentLinkIndex: 0,
        };
    },
    mutations: {
        updateLanguage(state, lang) {
            state.language = lang;
            window.localStorage.setItem('lang', lang);
        },
        updateArticleTree(state, data) {
            if (data) {
                state.articleTree = data || [];
                return;
            }
            setTimeout(() => {
                const t = document.getElementsByTagName('t');
                const articleTree = Array.from(t).filter((d) => d.id);
                state.articleTree = articleTree || [];
            }, 0);
        },
        updateCurrentLinkIndex(state, index) {
            state.currentLinkIndex = index;
        },
    },
});
